package com.wushi.lenovo.asia5b.app_ui.other.dialog.trans;

import android.view.View;

import com.mvp.base.model.ResultModel;
import com.mvp.utils.DecimalUtil;
import com.mvp.utils.StringUtils;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_event.OrderPayEvent;
import com.wushi.lenovo.asia5b.app_model.credit.bean.TopupInfoBean;
import com.wushi.lenovo.asia5b.app_model.other.TransItemBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_utils.DeviceIdUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * Created by zhangyuncai on 2019/11/27.
 * 订单付款(余额充足的情况下)
 */
public class OrderPayDFragment extends PayDialogFragment {
    TopupInfoBean topupInfoBean;
    Map<String, Object> map;

    public void setData(TopupInfoBean topupInfoBean) {
        this.topupInfoBean = topupInfoBean;
        map = MapHelper.newHashMap();
        map.put("member_id", UserInfo.getMemberId());
        map.put("checkout_log_id", topupInfoBean.getCheckout_log_id());
    }

    @Override
    protected void initView() {
        super.initView();
        setViewForPaySuff();
    }

    private void setViewForPaySuff() {
        mDatas.clear();
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_creditbalance), topupInfoBean.getBalance_value()));//账户金额
        mDatas.add(new TransItemBean(getString(R.string.withdraw_totalamount), StringUtils.formatNum2(topupInfoBean.getTotalAmount())));//转账金额
        String totalBalance = DecimalUtil.subtract(topupInfoBean.getBalance_value(), topupInfoBean.getTotalAmount());
        mDatas.add(new TransItemBean(getString(R.string.c_transfer_totalbalance), StringUtils.formatNum2(totalBalance)));//转账后余额
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected Observable<ResultModel> getSubmitObservable(String payPass) {
        map.put("topup_password", "" + payPass);
        DeviceIdUtil.addDevice(map);
        return topupService.submitTopupForPayment(map);
    }

    @Override
    protected Consumer<ResultModel> getSubmitResult() {
        return resultModel -> {
            if (resultModel.isSuccess()) {
                //todo 充值/订单支付余额充足
                OrderPayEvent orderPayEvent = new OrderPayEvent(resultModel);
                orderPayEvent.topupInfoBean = topupInfoBean;
                EventBus.getDefault().postSticky(orderPayEvent);
            }
            int successText = R.string.paymentsuccessful;
            int errorText = R.string.paymentfailed;
            ll_trans_notice_vessel.setVisibility(View.VISIBLE);
            if (resultModel.isSuccess()) {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_right);
                tv_trans_notice.setText(successText);
            } else {
                iv_trans_noticeicon.setImageResource(R.drawable.icon_trans_error);
                tv_trans_notice.setText(errorText);
            }
        };
    }
}
